package com.zyh.server.rabbitmq;

import com.zyh.server.config.MQConfig;
import com.zyh.server.entity.MiaoshaMsg;
import com.zyh.server.utils.BeanUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @Author: zyh
 * @Date: 2024-11-20-15:33
 * @Description 消息发送者
 */
@Slf4j
@Service
public class MQSender {
	@Autowired
	private RabbitTemplate rabbitTemplate;

	public void send(MiaoshaMsg msg){
		// 将对象转换为字符串
		String str = BeanUtil.beanToString(msg);
		// 调用发送的方法
		log.info("发送消息：{}",str);
		/**
		 * 参数一：路由key（队列名称）
		 * 参数二：消息内容
		 */
		rabbitTemplate.convertAndSend(MQConfig.miaosha_queue,str);
	}
}
